#include <vector>
#include <fstream>
#include <string>
#include <iostream>
#include "StringFunctions.h"

using std::vector;
using std::string;
using std::cout;
using std::endl;
using std::ifstream;

//opens the file and reads all the lines into a vector
//filename defined within this function, not passed
vector<string> readFile(){
    //create a vector of strings
    vector<string> lines;

    //open the text file fstream
    ifstream myfile("vplog.gz");
    //2. Until you reach end of file, read one line at a time
    if(myfile.is_open()){
        string line;

        while(getline(myfile, line)){
            //add each line to the vector
            lines.push_back(line);
        }
    }
    return lines;
}

//takes a copy of a string vector and prints each item on a separate line
void printVector(const vector<string>& s){

    for(vector<string>::size_type i = 0; i != s.size(); i++){
        cout << s[i] << endl;
    }
}

//returns the application version from a vector of lines
string findVersion(const vector<string>& s){

    // separate each line into words
    for(vector<string>::size_type i = 0; i != s.size(); i++){

        //create a string vector of words within the line
        vector<string> test = split(s[i]);

        //test each word in the vector of words for the keyword
        for(vector<string>::size_type j = 0; j != test.size(); j++){
            if (test[j] == "Application" ){
                return test[j+2];
            }
        }
    }
}

//returns a string vector containing all the page transition times
vector<string> getPageTrans(const vector<string>& s){

    vector<string> ret;

    for(vector<string>::size_type i = 0; i != s.size(); i++){

        vector<string> test = split(s[i]);

        for(vector<string>::size_type j = 0; j != test.size(); j++){
            if (test[j] == "Timing" ){
                // add the timing string to the vector
                ret.push_back(test[j+1] + " " + test[j+2] + " " + test[j+3] + " " + test[j+4]);
            }
        }
    }
    return ret;
}


// main function
int main(int argc, char* argv[])
{
    //read the file into a vector of lines
    vector<string> lines;

    lines = readFile();

    cout << "Application Version: " <<  findVersion(lines) << endl;
    cout << "Page Transitions: " << endl;
    printVector(getPageTrans(lines));

    return 0;
}
